﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service;
using System.Security.Cryptography;
using MediatR;
using RBAC.MaHaiBo.API.Application.Command.UserRoleCommand;
using Microsoft.AspNetCore.Authorization;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 用户与角色关联控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserRoleController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<UserRoleController> logger;
        /// <summary>
        /// 用户与角色关联服务层接口
        /// </summary>
        IUserRoleService urSer;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="urSer"></param>
        public UserRoleController(ILogger<UserRoleController> logger,
        IUserRoleService urSer,
           IMediator mediator)
        {
            this.logger = logger;
            this.urSer = urSer;
            this.mediator = mediator;
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ByAddUserRole(UserRoleCreateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【用户与角色服务层】: 添加用户与角色 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Uid">用户编号</param>
        /// <param name="Rid">角色编号</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DelUserRole([FromQuery] UserRoleDeleteCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【用户与角色服务层】: 删除用户与角色 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 根据部门显示用户
        /// </summary>
        /// <param name="Ids">部门编号集合</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Tmeen>>> GetUserinfo([FromQuery]UserRoleShowCommand request)
        {
            ApiResult<List<Tmeen>> result = new ApiResult<List<Tmeen>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【用户与角色服务层】: 根据部门显示用户 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 根据角色显示
        /// </summary>
        /// <param name="Rid">角色编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<UserInfo>>> GetRoleUserinfo([FromQuery] UserRoleShow1Command request)
        {
            ApiResult<List<UserInfo>> result = new ApiResult<List<UserInfo>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【用户与角色服务层】: 根据角色显示 出错" + ex.Message);
                throw;
            }
            return result;

        }

        /// <summary>
        /// 反填用户角色显示
        /// </summary>
        /// <param name="Rid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<int>>> Backfillde([FromQuery] UserRoleBackfillCommandd request)
        {
            ApiResult<List<int>> result = new ApiResult<List<int>>();
            try
            {
                result.Data = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【用户与角色服务层】: 反填用户角色显示 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填用户角色显示（用户）
        /// </summary>
        /// <param name="Rid">角色</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<int>>> Backfilluid([FromQuery] UserRoleBackfill1Commandd request)
        {
            ApiResult<List<int>> result = new ApiResult<List<int>>();
            try
            {
                result.Data = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【用户与角色服务层】: 反填用户角色显示 出错" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
